//
//  WBEmoticonPageViewCell.swift
//  Weibo
//
//  Created by xu on 2017/3/30.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit

class WBEmoticonPageViewCell: UICollectionViewCell {

    // 提供一个属性 供外界赋值
    var emoticons: [WBEmoticonModel]? {
        didSet{
            // 遍历按钮数组 隐藏按钮
            for button in emoticonButtons {
                button.isHidden = true
            }
            // 赋值
            // 遍历外界传入的一维模型数组
            for (i,emoticonModel)in emoticons!.enumerated() {
                // 通过角标 获取对应按钮
                let button = emoticonButtons[i]
               //赋值
                button.emoticonModel = emoticonModel
            }

        }
    }
    
    
   
    // 模型数组

    
     // 保存20个表情按钮
    var emoticonButtons:[WBEmoticonButton] = [WBEmoticonButton]()
    
    
    //重写
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    private func setupUI(){
        addChildButtons()
        contentView.addSubview(messageLabel)
        contentView.addSubview(deleteButton)
        messageLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
        

    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let buttonW: CGFloat = (ScreenWidth - 10)/CGFloat(HMEMOTICONMAXCOL)
        let buttonH: CGFloat = (216 - 35 - 20)/CGFloat(HMEMOTICONMAXROW)
        // 正向遍
        for (i,button) in emoticonButtons.enumerated() {
            // 计算列数和行数
            let col = CGFloat(i%HMEMOTICONMAXCOL)
            let row = CGFloat(i/HMEMOTICONMAXCOL)
            button.frame = CGRect(x:col*buttonW + 5, y:row*buttonH, width:buttonW, height:buttonH)

        }
        
    
        deleteButton.frame = CGRect(x: ScreenWidth - buttonW - 5, y: buttonH*2, width: buttonW, height: buttonH)
        
    }
    
    
    
    //删除按钮
    
    lazy var deleteButton :UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named:"compose_emotion_delete"), for: UIControlState.normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
       
        button.addTarget(self, action: #selector(deleteButtonClick), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    
    //懒加载控件
    lazy var messageLabel :UILabel = {
       
        let lab = UILabel(title: "", fontSize: 30, textColor: UIColor.red)
        return lab
        
    }()
    
}



//添加20按钮
extension WBEmoticonPageViewCell{
    fileprivate func addChildButtons() {
        
        for _ in 0..<HMEMOTICONMAXCOUNT{
            let button = WBEmoticonButton()
            button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
            //按钮添加点击事件
            button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
            //添加数组中
            emoticonButtons.append(button)
            
            contentView.addSubview(button)
        
        }
        
    }
    
    
    
}

//MARK:  按钮点击方法实现
extension WBEmoticonPageViewCell {
    
    @objc fileprivate func buttonClick (button:WBEmoticonButton){
        
        //发送通知给textview(控制器) 通知将按钮的模型属性传递过去
        NotificationCenter.default.post(name: NSNotification.Name(rawValue:EMOTICONBUTTONCLICKNOTI), object: button.emoticonModel)

    }

    
    //删除按钮
    @objc  fileprivate func deleteButtonClick(button:UIButton){
         NotificationCenter.default.post(name: NSNotification.Name(rawValue:EMOTICONDELETEBUTTONCLICK), object: nil)
    }
    
    
}
